@echo off
echo ===== 第二课堂管理系统菜单优化脚本 =====
echo 此脚本将优化系统菜单结构并重启系统

REM 设置数据库连接参数
set DB_USER=root
set DB_PASSWORD=123456
set DB_NAME=scms
set DB_HOST=localhost
set DB_PORT=3306

echo 数据库连接信息:
echo 用户名: %DB_USER%
echo 数据库: %DB_NAME%
echo 主机: %DB_HOST%:%DB_PORT%
echo.

echo 步骤1: 检查并停止已运行的实例...
taskkill /f /im java.exe 2>nul
if %ERRORLEVEL% equ 0 (
    echo Java进程已终止
) else (
    echo 未找到正在运行的Java进程
)

echo 步骤2: 执行菜单优化SQL命令...

echo -- 调整教师管理和学生管理的顺序 > temp_menu_optimize.sql
echo UPDATE sys_menu SET order_num = 10 WHERE menu_id = 2072; >> temp_menu_optimize.sql
echo UPDATE sys_menu SET order_num = 11 WHERE menu_id = 2073; >> temp_menu_optimize.sql
echo. >> temp_menu_optimize.sql
echo -- 添加菜单权限标识 >> temp_menu_optimize.sql
echo UPDATE sys_menu SET perms = 'secondclass:activity:list' WHERE menu_id = 2069; >> temp_menu_optimize.sql
echo UPDATE sys_menu SET perms = 'secondclass:enrollment:list' WHERE menu_id = 2070; >> temp_menu_optimize.sql
echo UPDATE sys_menu SET perms = 'secondclass:credit:list' WHERE menu_id = 2071; >> temp_menu_optimize.sql
echo. >> temp_menu_optimize.sql
echo -- 确保管理员角色拥有所有必要菜单的权限 >> temp_menu_optimize.sql
echo INSERT IGNORE INTO sys_role_menu ^(role_id, menu_id^) >> temp_menu_optimize.sql
echo SELECT 1, menu_id FROM sys_menu >> temp_menu_optimize.sql
echo WHERE menu_id IN ^(2068, 2069, 2070, 2071, 2072, 2073^) >> temp_menu_optimize.sql
echo AND menu_id NOT IN ^(SELECT menu_id FROM sys_role_menu WHERE role_id = 1^); >> temp_menu_optimize.sql

mysql -u %DB_USER% -p%DB_PASSWORD% -h %DB_HOST% -P %DB_PORT% %DB_NAME% --default-character-set=utf8 < temp_menu_optimize.sql

if %ERRORLEVEL% equ 0 (
    echo SQL命令执行成功！
) else (
    echo SQL命令执行失败，请检查错误信息。
    goto :error
)

del temp_menu_optimize.sql

echo 步骤3: 启动系统...
cd scms-admin
start cmd /k mvn spring-boot:run
cd ..

cd scms-ui
start cmd /k npm run dev
cd ..

echo ===== 菜单优化完成！=====
echo 系统已重新启动，请刷新浏览器查看优化后的菜单结构。
goto :end

:error
echo 菜单优化过程中出现错误，请查看上面的错误信息。

:end
echo 详细的优化报告请查看 menu_optimization_report.md
pause 